package com.example.medical.controller.admin;

import com.example.medical.entity.dto.*;
import com.example.medical.service.AdminService;
import com.example.medical.utils.common.enums.Code;
import com.example.medical.utils.common.result.Result;
import com.example.medical.utils.config.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @PostMapping("/admin_login")
    public Result AdminLogin(@RequestBody LoginDto dto){
        return adminService.AdminLogin(dto);
    }

    @PostMapping("/admin_logout")
    public Result AdminLogout(){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        BaseContext.removeCurrentId();
        return Result.okResult(Code.SUCCESS);
    }

    //list

    @PostMapping("/get_admin_list")
    public Result GetAdminList(@RequestBody GetAdminListDto dto){
        return adminService.GetAdminList(dto);
    }

    @GetMapping("/remove_admin")
    public Result RemoveAdmin(@RequestParam("id")Long id){
        return adminService.RemoveAdmin(id);
    }

    @GetMapping("/get_admin_detail")
    public Result GetAdminDetail(@RequestParam("id") Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(adminService.getById(id));
    }

    @PostMapping("/save_admin")
    public Result SaveAdmin(@RequestBody SaveAdminDto dto){
        return adminService.SaveAdmin(dto);
    }

    @GetMapping("/update_get_admin")
    public Result UpdateGetAdmin(@RequestParam("id") Long id){
        return Result.okResult(adminService.getById(id));
    }

    @PostMapping("/update_admin")
    public Result UpdateAdmin(@RequestBody UpdateAdminDto dto){
        return adminService.UpdateAdmin(dto);
    }

    @GetMapping("/remove_admin_arr")
    public Result RemoveAdminArr(@RequestParam("ids") String[] ids){
        return adminService.RemoveAdminArr(ids);
    }

    //me

    @GetMapping("/get_admin")
    public Result GetMe(){
        return Result.okResult(adminService.getById(BaseContext.getCurrentId()));
    }

    @PostMapping("/update_admin_password")
    public Result UpdateMePassword(@RequestBody UpdatePasswordDto dto){
        return adminService.UpdateAdminPassword(dto);
    }

    @GetMapping("/update_get_admin_message")
    public Result AdminUpdateGetMeMessage(){
        return Result.okResult(adminService.getById(BaseContext.getCurrentId()));
    }

    @GetMapping("/update_admin_message")
    public Result AdminUpdateMeMessage(@RequestParam("name")String name){
        return adminService.AdminUpdateMeMessage(name);
    }



}
